home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Source.bin / ImagePanel.java < prev    next >
Text File  |  1998-10-23  |  9KB  |  352 lines

  1. package symantec.itools.awt;
  2.  
  3. import java.awt.Panel;
  4. import java.net.URL;
  5. import java.awt.Image;
  6. import java.awt.MediaTracker;
  7. import java.awt.Graphics;
  8. import java.awt.Dimension;
  9. import java.beans.PropertyVetoException;
  10. import java.beans.PropertyChangeListener;
  11. import java.beans.VetoableChangeListener;
  12. import java.beans.PropertyChangeEvent;
  13.  
  14. //     03/02/97    RKM    Changed call to invalidate to repaint
  15. //    05/31/97    RKM    Updated to support Java 1.1
  16. //                    Made properties bound & constrained
  17. //  07/29/97    CAR marked fields transient as needed
  18. //                  implemented readObject
  19. //    05/31/97    RKM    Fixed bug in setImageURL, was not properly comparing objects
  20. //  08/19/97    CAR constructor now calls super.setLayout(null)
  21. //  08/25/97    LAB Changed all property name strings in bound and constrained messages to
  22. //                    conform to the BeanSpec naming guidelines.  Added IMAGE_NORMAL image style
  23. //                    (Addresses Mac Bug #7255).
  24. //  08/27/97    CAR updated setImageURL to "erase" image if URL is null
  25. //                  updated paint to draw images in itself before calling super.paint
  26. //  09/11/97    LAB Made imageStyle protected (Addresses Mac Bug #7692).
  27.  
  28. /**
  29.  * The ImagePanel component is similar to a regular panel except that it
  30.  * displays an image within the panel.
  31.  * The image to use is specified with a URL.
  32.  */
  33. public class ImagePanel extends java.awt.Panel
  34. {
  35.     /**
  36.      * A constant indicating the image is to be tiled in this panel.
  37.      */
  38.     public static final int IMAGE_TILED = 0;
  39.     /**
  40.      * A constant indicating the image is to be centered in this panel.
  41.      */
  42.     public static final int IMAGE_CENTERED = 1;
  43.     /**
  44.      * A constant indicating the image is to be scaled to fit this panel.
  45.      */
  46.     public static final int IMAGE_SCALED_TO_FIT = 2;
  47.     /**
  48.      * A constant indicating the image is to be drawn from the upper left corner of the panel.
  49.      */
  50.     public static final int IMAGE_NORMAL = 3;
  51.  
  52.     /**
  53.      * Constructs a default ImagePanel. By default the image will be tiled.
  54.      */
  55.     public ImagePanel()
  56.     {
  57.         super.setLayout(null);
  58.         imageURL = null;
  59.         image = null;
  60.         imageStyle = IMAGE_TILED;
  61.  
  62.         vetos = new symantec.itools.beans.VetoableChangeSupport(this);
  63.         changes = new symantec.itools.beans.PropertyChangeSupport(this);
  64.     }
  65.  
  66.     // Properties
  67.  
  68.     /**
  69.      * Paints this component using the given graphics context.
  70.      * This is a standard Java AWT method which typically gets called
  71.      * by the AWT to handle painting this component. It paints this component
  72.      * using the given graphics context. The graphics context clipping region
  73.      * is set to the bounding rectangle of this component and its [0,0]
  74.      * coordinate is this component's top-left corner.
  75.      *
  76.      * @param g the graphics context used for painting
  77.      * @see java.awt.Component#repaint
  78.      * @see java.awt.Component#update
  79.      */
  80.     public void paint(Graphics g)
  81.     {
  82.         Dimension dim = size();
  83.         if (image != null)
  84.         {
  85.  
  86.             int imageWidth = image.getWidth(this);
  87.             int imageHeight = image.getHeight(this);
  88.  
  89.             switch(imageStyle)
  90.             {
  91.                 default:
  92.                 case IMAGE_TILED:
  93.                 {
  94.                     //Calculate number of images that should be drawn horizontally
  95.                     int numHImages = dim.width / imageWidth;
  96.  
  97.                     //Don't forget remainders
  98.                     if (dim.width % imageWidth != 0)
  99.                         numHImages++;
  100.  
  101.                     //Calculate number of images that should be drawn vertically
  102.                     int numVImages = dim.height / imageHeight;
  103.  
  104.                     //Don't forget remainders
  105.                     if (dim.height % imageHeight != 0)
  106.                         numVImages++;
  107.  
  108.                     int h;
  109.                     int v = 0;
  110.                     for (int vCount = 0;vCount < numVImages;vCount++)
  111.                     {
  112.                         h = 0;
  113.                         for (int hCount = 0;hCount < numHImages;hCount++)
  114.                         {
  115.                             g.drawImage(image, h, v, imageWidth, imageHeight, this);
  116.  
  117.                             //Increment to next column
  118.                             h += imageWidth;
  119.                         }
  120.  
  121.                         //Increment to next row
  122.                         v += imageHeight;
  123.                     }
  124.  
  125.                     break;
  126.                 }
  127.  
  128.                 case IMAGE_CENTERED:
  129.                 {
  130.                     g.drawImage
  131.                         (image,
  132.                          (dim.width - imageWidth) / 2,
  133.                          (dim.height - imageHeight) / 2,
  134.                          imageWidth,
  135.                          imageHeight,
  136.                          this);
  137.  
  138.                     break;
  139.                 }
  140.  
  141.                 case IMAGE_SCALED_TO_FIT:
  142.                 {
  143.                     g.drawImage(image, 0, 0, dim.width, dim.height, this);
  144.  
  145.                     break;
  146.                 }
  147.  
  148.                 case IMAGE_NORMAL:
  149.                 {
  150.                     g.drawImage(image, 0, 0, this);
  151.  
  152.                     break;
  153.                 }
  154.             }//switch
  155.         }
  156.         else
  157.         {
  158.             g.clearRect(0, 0, dim.width, dim.height);
  159.         }
  160.         super.paint(g);
  161.     }
  162.  
  163.     /**
  164.      * Sets the URL of the image to display in this panel.
  165.      * @param url the URL of the image to display
  166.      * @see #getImageURL
  167.      * @exception PropertyVetoException
  168.      * if the specified property value is unacceptable
  169.      */
  170.     public void setImageURL(URL url)
  171.         throws PropertyVetoException
  172.     {
  173.         if (!symantec.itools.util.GeneralUtils.objectsEqual(imageURL,url))
  174.         {
  175.             vetos.fireVetoableChange("imageURL", imageURL, url);
  176.  
  177.             imageURL = url;
  178.             if (imageURL != null)
  179.             {
  180.                 if(image != null)
  181.                 {
  182.                     image.flush();
  183.                     image = null;
  184.                 }
  185.                 image = getToolkit().getImage(imageURL);
  186.                 if (image != null)
  187.                 {
  188.                     MediaTracker mt = new MediaTracker(this);
  189.                     try
  190.                     {
  191.                         mt.addImage(image, 0);
  192.                         mt.waitForAll();
  193.                     }
  194.                     catch (InterruptedException ie)
  195.                     {
  196.                     }
  197.                 }
  198.             }
  199.             else
  200.             {
  201.                 if(image != null)
  202.                 {
  203.                     image.flush();
  204.                     image = null;
  205.                 }
  206.  
  207.             }
  208.  
  209.             changes.firePropertyChange("imageURL", imageURL, url);
  210.  
  211.             repaint();
  212.         }
  213.     }
  214.  
  215.     /**
  216.      * Returns the URL of the image being displayed in this panel.
  217.      * @see #setImageURL
  218.      */
  219.     public URL getImageURL()
  220.     {
  221.         return imageURL;
  222.     }
  223.  
  224.     /**
  225.      * Sets the new panel image style.
  226.      * @param newStyle the new panel image style, one of
  227.      * IMAGE_TILED, IMAGE_CENTERED, or IMAGE_SCALED_TO_FIT
  228.      * @exception PropertyVetoException
  229.      * if the specified property value is unacceptable
  230.      * @see #getStyle
  231.      * @see #IMAGE_TILED
  232.      * @see #IMAGE_CENTERED
  233.      * @see #IMAGE_SCALED_TO_FIT
  234.      * @see #IMAGE_NORMAL
  235.      */
  236.     public void setStyle(int newStyle) throws PropertyVetoException
  237.     {
  238.         if (newStyle != imageStyle)
  239.         {
  240.             Integer oldStyleInt = new Integer(imageStyle);
  241.             Integer newStyleInt = new Integer(newStyle);
  242.  
  243.             vetos.fireVetoableChange("style", oldStyleInt, newStyleInt);
  244.  
  245.             imageStyle = newStyle;
  246.  
  247.             changes.firePropertyChange("style", oldStyleInt, newStyleInt);
  248.  
  249.             repaint();
  250.         }
  251.     }
  252.  
  253.     /**
  254.      * Gets the current panel image style.
  255.      * @return the current panel image style, one of
  256.      * IMAGE_TILED, IMAGE_CENTERED, or IMAGE_SCALED_TO_FIT
  257.      * @see #setStyle
  258.      * @see #IMAGE_TILED
  259.      * @see #IMAGE_CENTERED
  260.      * @see #IMAGE_SCALED_TO_FIT
  261.      * @see #IMAGE_NORMAL
  262.      */
  263.     public int getStyle()
  264.     {
  265.         return imageStyle;
  266.     }
  267.  
  268.     // Methods
  269.  
  270.     //???RKM??? When beans come around we need to make certain that this is not returned as a property
  271.     /**
  272.      * Returns the image being displayed in this panel.
  273.      */
  274.     public Image getImage()
  275.     {
  276.         return image;
  277.     }
  278.  
  279.     /**
  280.      * Adds a listener for all event changes.
  281.      * @param listener the listener to add.
  282.      * @see #removePropertyChangeListener
  283.      */
  284.     public synchronized void addPropertyChangeListener(PropertyChangeListener listener)
  285.     {
  286.         //super.addPropertyChangeListener(listener);
  287.         changes.addPropertyChangeListener(listener);
  288.     }
  289.  
  290.     /**
  291.      * Removes a listener for all event changes.
  292.      * @param listener the listener to remove.
  293.      * @see #addPropertyChangeListener
  294.      */
  295.     public synchronized void removePropertyChangeListener(PropertyChangeListener listener)
  296.     {
  297.         //super.removePropertyChangeListener(listener);
  298.         changes.removePropertyChangeListener(listener);
  299.     }
  300.  
  301.     /**
  302.      * Adds a vetoable listener for all event changes.
  303.      * @param listener the listener to add.
  304.      * @see #removeVetoableChangeListener
  305.      */
  306.     public synchronized void addVetoableChangeListener(VetoableChangeListener listener)
  307.     {
  308.          //super.addVetoableChangeListener(listener);
  309.         vetos.addVetoableChangeListener(listener);
  310.     }
  311.  
  312.     /**
  313.      * Removes a vetoable listener for all event changes.
  314.      * @param listener the listener to remove.
  315.      * @see #addVetoableChangeListener
  316.      */
  317.     public synchronized void removeVetoableChangeListener(VetoableChangeListener listener)
  318.     {
  319.         //super.removeVetoableChangeListener(listener);
  320.         vetos.removeVetoableChangeListener(listener);
  321.     }
  322.  
  323.     private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, ClassNotFoundException {
  324.         in.defaultReadObject();
  325.  
  326.         if (imageURL != null) {
  327.             image = getToolkit().getImage(imageURL);
  328.             if (image != null) {
  329.                 MediaTracker mt = new MediaTracker(this);
  330.                 try {
  331.                     mt.addImage(image, 0);
  332.                     mt.waitForAll();
  333.                 }
  334.                 catch (InterruptedException ie) { }
  335.             }
  336.         }
  337.     }
  338.  
  339.     /**
  340.      * The style that the image will be displayed in.
  341.      */
  342.     protected int imageStyle;
  343.     
  344.     // Private members
  345.  
  346.     transient private Image image;
  347.     private URL imageURL;
  348.     private symantec.itools.beans.VetoableChangeSupport vetos;
  349.     private symantec.itools.beans.PropertyChangeSupport changes;
  350. }
  351.  
  352.